/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bookstore.dao;

import bookstore.idao.IOrderDetailsDao;
import bookstore.model.Book;
import bookstore.model.Category;
import bookstore.model.Order;
import bookstore.model.Orderdetails;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author MY
 */
public class OrderDetailsDao implements IOrderDetailsDao {

    @Override
    public void deleteDetails(Integer orderId) {

    }

    @Override
    public List<Orderdetails> findDetails(Order order) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Orderdetails details = null;
        Book book = null;
        Category category = null;
        List<Orderdetails> detialsList = new ArrayList<Orderdetails>();
        try {
            con = MySQLHelper.connect();
            String sql = "select a.*,b.*,c.cat_name from bs_details a,bs_book b,bs_category c where a.book_id=b.book_id and b.cat_id =c.cat_id and a.ord_id=" + order.getId() + "";
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                details = new Orderdetails();
                book = new Book();
                category = new Category();
                category.setName(rs.getString("cat_name"));
                book.setId(rs.getInt("book_id"));
                category.setId(rs.getInt("cat_id"));
                book.setCategory(category);
                book.setName(rs.getString("book_name"));
                book.setPublisher(rs.getString("book_publisher"));
                book.setIsbn(rs.getString("book_isbn"));
                book.setPicture(rs.getString("book_picture"));
                book.setPrice(rs.getFloat("book_price"));
                book.setAuthor(rs.getString("book_author"));
                book.setDesc(rs.getString("book_desc"));
                book.setCount(rs.getInt("book_count"));
                book.setDiscount(rs.getInt("book_discount"));
                details.setBook(book);
                details.setCount(rs.getInt("ordd_count"));
                details.setId(rs.getInt("ordd_id"));
                details.setOrder(order);
                details.setPrice(rs.getFloat("ordd_price"));
                details.setTotal(rs.getFloat("ordd_total"));
                detialsList.add(details);
                System.out.println("asdf sdf asd sadf asdf afs"+details.getTotal());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }

        return detialsList;
    }

    @Override
    public int findAllCount() {
        return 0;
    }

    @Override
    public List<Orderdetails> findAll() {

        return null;
    }

    @Override
    public Orderdetails findById(Integer orderId) {
        return null;
    }

    @Override
    public void delete(Integer orderId) {

    }

    @Override
    public void updatePassword(Orderdetails detail) {

    }

    @Override
    public void updateMessage(Orderdetails detail) {

    }

    @Override
    public void update(Orderdetails detail) {

    }

    @Override
    public void insert(Orderdetails detail) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = MySQLHelper.connect();
            String sql = "insert into bs_details(book_id,ordd_price,ordd_count,ordd_total,ord_id) values(?,?,?,?,?)";
            ps = con.prepareStatement(sql);
            ps.setInt(1, detail.getBook().getId());
            ps.setFloat(2, detail.getBook().getPrice());
            ps.setInt(3, detail.getCount());
            ps.setFloat(4, detail.getTotal());
            ps.setInt(5, detail.getOrder().getId());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
    }
}
